<?php
declare (strict_types=1);

namespace app\mall\service\order\admin;

use app\admin\logic\system\Log;
use app\common\library\Excel;
use app\common\service\BaseService;
use app\mall\logic\order\admin\Order as OrderLogic;
use eduline\admin\libs\pageform\FormItem;
use eduline\admin\libs\pagelist\ListItem;
use eduline\admin\page\PageList;
use think\annotation\Inject;
use think\facade\Request;
use think\Response;

class Order extends BaseService
{

    /**
     * @Inject()
     * @var OrderLogic
     */
    protected $orderLogic;

    public function list(): Response
    {
        // 搜索功能
        $map = [];
        // 订单号
        if (input('?get.order_no') && $orderNo = input('get.order_no/s')) $map[] = ['main.order_no', '=', $orderNo];
        // 商品名称
        if (input('?get.title') && $title = input('get.title/s')) $map[] = ['product.title', 'LIKE', '%' . $title . '%'];
        // 下单手机号
        if (input('?get.phone') && $phone = input('get.phone/d')) $map[] = ['main.addrinfo->phone', '=', $phone];
        // 下单时间区间
        if (input('?get.date') && $date = input('get.date/a')) {
            $date  = array_map(function ($i) {
                return strtotime($i);
            }, $date);
            $map[] = ['order.create_time', 'between', $date];
        }
        // 数据
        $data = $this->orderLogic->getList($map);
        // 定义字段
        $keyList   = [
            'user_name'    => ListItem::make()->title('下单用户'),
            'order_no'     => ListItem::make()->title('订单号'),
            'title'        => ListItem::make()->title('商品名称'),
            'consignee'    => ListItem::make()->title('收货人'),
            'phone'        => ListItem::make()->title('收货人手机号'),
            'num'          => ListItem::make()->title('下单数量'),
            'create_time'  => ListItem::make()->title('下单时间'),
            'status'       => ListItem::make('custom')->title('订单状态'),
            'payment_time' => ListItem::make()->title('支付时间'),
        ];
        $searchKey = [
            'order_no' => FormItem::make()->title(false)->placeholder('订单号')->maxlength(30),
            'title'    => FormItem::make()->title(false)->placeholder('商品名称')->maxlength(30),
            'phone'    => FormItem::make()->title(false)->placeholder('下单手机号')->maxlength(30),
            'date'     => FormItem::make('date')->title(false)->dateType(FormItem::option('DateType', 'DATETIME_RANGE'))->startPlaceholder()->endPlaceholder()->format('yyyy-MM-dd HH:mm'),
        ];
        // 设置表单
        $list = app(PageList::class);
        // 表单字段
        $list->pageKey   = $keyList;
        $list->searchKey = $searchKey;
        $list->datas     = $data;

        return $list->send();
    }

    public function detail($order_no): Response
    {
        $data = $this->orderLogic->getDetail($order_no);

        return static::parseToData($data, 1, '获取成功');
    }

    public function express($order_sub_no)
    {
        $param = Request::post();
        $this->orderLogic->editExpress($order_sub_no, $param);

        return static::parseToData([], 1, '获取成功');
    }

    public function export()
    {
        // 搜索功能
        $map = [];
        // 订单号
        if (input('?post.order_no') && $orderNo = input('post.order_no/s')) $map[] = ['main.order_no', '=', $orderNo];
        // 商品名称
        if (input('?post.title') && $title = input('post.title/s')) $map[] = ['product.title', 'LIKE', '%' . $title . '%'];
        // 下单手机号
        if (input('?post.phone') && $phone = input('post.phone/d')) $map[] = ['main.addrinfo->phone', '=', $phone];
        // 下单时间区间
        if (input('?post.date') && $date = input('post.date/a')) {
            $date  = array_map(function ($i) {
                return strtotime($i);
            }, $date);
            $map[] = ['order.create_time', 'between', $date];
        }
        // 数据
        $data = $this->orderLogic->getList($map);

        $xlsCell = [
            ['user_name', '下单用户'],
            ['order_no', '订单号'],
            ['title', '商品名称'],
            ['consignee', '收货人'],
            ['phone', '收货人手机号'],
            ['num', '下单数量'],
            ['create_time', '下单时间'],
            ['status_text', '订单状态'],
            ['payment_time', '支付时间'],
            ['payment_type_text', '支付方式'],
            ['out_trade_no', '商户订单'],
        ];

        Log::write('导出商城订单', 'mall_order', 'export', true);

        return Excel::exportExcel('商城订单', $xlsCell, $data);
    }
}